/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.hamacekj.geometr;

import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JTextField;
import javax.swing.text.Document;

/**
 * Textové pole, které je schopné zobrazit placeholder.
 * @author hamacekh
 */
public class PlaceholderField extends JTextField {

    public PlaceholderField() {
    }

    public PlaceholderField(String text) {
        super(text);
    }

    public PlaceholderField(int columns) {
        super(columns);
    }

    public PlaceholderField(String text, int columns) {
        super(text, columns);
    }

    public PlaceholderField(Document doc, String text, int columns) {
        super(doc, text, columns);
    }
    
    
    private String placeholder = null;
    
    /**
     * Nastaví text placeholderu.
     * @param placeholder text placeholderu.
     */
    public void setPlaceholder(String placeholder){
        this.placeholder = placeholder;
        this.setToolTipText(placeholder);
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        if(placeholder != null && this.getText().isEmpty()){
            Graphics2D gr = (Graphics2D)g;
            gr.setPaint(Color.GRAY);
            FontMetrics metrics = gr.getFontMetrics();
            int fontHeight = metrics.getAscent();
            gr.drawString(placeholder, 3, fontHeight);
        }
    }
    
    
}
